import { reqCartList, reqDeleteCartById, reqUpdateCheckedById, reqCartToOrder ,
    reqClearCart ,reqExtendOrdersList,reqChangeExtendOrdersState,reqExtendOrdersId} from "@/api"
const state = {
    cartList: [],
    extendOrdersList: [],
};
const mutations = {
    GETCARTLIST(state, cartList) {
        state.cartList = cartList;
    },
    EXTENDORDERSLIST(state, extendOrdersList){
        state.extendOrdersList=extendOrdersList;
    },
};
const actions = {
    //获取购物车列表数据
    async getCartList({ commit }) {
        let result = await reqCartList();
        if (result.code == 200) {
            commit("GETCARTLIST", result.data)
        }
    },
    //删除购物车的一个产品
    async deleteCartByWaresId({ commit }, cartId) {
        let result = await reqDeleteCartById(cartId);
        if (result.code == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //删除购物车的选中产品
    deleteAllCheckedCart({dispatch,getters}) {
        let PromiseAll = []
        getters.cartList.forEach(item=>{
         let promise=item.cartState==1?dispatch('deleteCartByWaresId',item.cartId):'';
         //将每次返回的promise添加到数组中
         PromiseAll.push(promise);
        })
        return Promise.all(PromiseAll);
    },
    //修改购物车一个产品的选中状态
    async updateCheckerById({commit},{waresId, cartState}) {

        let result = await reqUpdateCheckedById(waresId, cartState);
        if (result.code == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //修改购物车所有产品的选中属性
    updateAllCartChecked({dispatch,getters},cartState) {
        let PromiseAll = []
        getters.cartList.forEach(item=>{
         let promise=dispatch('updateCheckerById',{waresId:item.waresId,cartState:cartState});
         //将每次返回的promise添加到数组中
         PromiseAll.push(promise);
        })
        return Promise.all(PromiseAll);
    },
    //提交购物车选中商品到订单
    async insertCartToOrder({ commit },{address,payment}) {
        let result = await reqCartToOrder(address,payment);
        if (result.code == 200) {
            return result.data;
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //删除购物车中以下架或冻结的商品
    async clearCart({commit}){
        let result = await reqClearCart();
        if(result.code == 200){
            return 'ok';
        }else{
            return Promise.reject(new Error('faile'));
        }
    },
    //获取所包含的订单数据
    async extendOrdersList({commit},ordersId){
        let result = await reqExtendOrdersList(ordersId);
        if(result.code == 200){
            commit("EXTENDORDERSLIST", result.data)
        }else{
            return Promise.reject(new Error('faile'));
        }
    },
    //修改所包含的订单状态
    async changeExtendOrdersState({commit},ordersId){
        let result = await reqChangeExtendOrdersState(ordersId);
        if(result.code == 200){
            return 'ok';
        }else{
            return Promise.reject(new Error('faile'));
        }
    },
    //根据订单id获取扩展订单id
    async extendOrdersId({commit},orderId){
        let result =await reqExtendOrdersId(orderId);
        if(result.code == 200){
            return result.data;
        }else{
            return Promise.reject(new Error('faile'));
        }
    }
};
const getters = {
    cartList(state) {
        return state.cartList || [];
    }
};
export default {
    state, mutations, actions, getters
}